home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / procssng / ccs / ccs-11tl.lha / lbl / hips / sources / isobuild / block.c next >
Encoding:
C/C++ Source or Header  |  1992-02-11  |  5.2 KB  |  205 lines

  1.  
  2. /* block.c              -Brian Tierney, LBL        1/91  */
  3.  
  4. /*  This is part of isobuild program.
  5.  *
  6.  *  This routine divide the data into blocks, with a structure indicating
  7.  *  the minimum, maximum, and location of each block of data.
  8.  *
  9.  *  Using this routines will usually result in a 30% speed up, because
  10.  *  large sections of the data set can be skipped.  There is an additional
  11.  *  use of memory however.
  12.  */
  13.  
  14.  /* 
  15.   *  This method used here is to create a 2D array of "BLOCK_INFO"
  16.   *   structures which contain the following infomation:
  17.   *     1) the location of the block in the 3D data set
  18.   *     2) the size of the block
  19.   *     3) the minimum and maximum values within the block
  20.   *     4) pointers into the data and grid structures
  21.   *
  22.   *  Using this information, blocks of data can be skipped by just
  23.   *  checking the minimum and maximum values within a block.
  24.  */
  25.  
  26.  
  27. /* $Id: block.c,v 1.3 1992/01/31 02:05:45 tierney Exp $ */
  28.  
  29. /* $Log: block.c,v $
  30.  * Revision 1.3  1992/01/31  02:05:45  tierney
  31.  * *** empty log message ***
  32.  *
  33.  * Revision 1.2  1991/12/19  01:41:16  davidr
  34.  * added RCS identification markers
  35.  * */
  36.  
  37. static char rcsid[] = "$Id: block.c,v 1.3 1992/01/31 02:05:45 tierney Exp $" ;
  38.  
  39. #include "isobuild.h"
  40.  
  41. /*********************************************************/
  42. int
  43. block_setup()
  44. {
  45.     int       nblocks;
  46.     BLOCK_INFO **alloc_block_info_array();
  47.  
  48.     /* guess a good block size: this will depend on the type of data
  49.        and the size of the structures relative to the size of the
  50.        data set.  I've found that this seems to be a good value for
  51.        medical data.  -BT
  52.      */
  53.  
  54.     if (BLOCK_SIZE == 0)
  55.     BLOCK_SIZE = MAX(xdim, ydim) / 12;
  56.     if (BLOCK_SIZE < 3)
  57.     BLOCK_SIZE = 3;
  58.  
  59.     fprintf(stderr, "Creating block min/max map with size %d blocks..\n",
  60.         BLOCK_SIZE);
  61.     nblocks = MAX(xdim, ydim) / BLOCK_SIZE;
  62.     if ((MAX(xdim, ydim) % BLOCK_SIZE) != 0)
  63.     nblocks++;
  64.     nblocks = nblocks * nblocks;
  65.     if (block_info_array == NULL)
  66.     block_info_array = alloc_block_info_array(zdim, nblocks);
  67.  
  68.     create_blocks(nblocks);
  69.  
  70.     get_block_min_max(nblocks);
  71.  
  72.     Status("Block map created.");
  73.  
  74.     return (nblocks);
  75. }
  76.  
  77. /********************************************************************/
  78. create_blocks(tot_num_blocks)  /* sets up block structures */
  79.     int       tot_num_blocks;
  80. {
  81.     register int slice, block;
  82.     BLOCK_INFO *binfo_ptr;
  83.     Grid_type **alloc_2d_grid_array();
  84.     int       xloc = 0, yloc = 0;
  85.     int       nxsize, nysize;
  86.  
  87.     for (slice = 0; slice < zdim; slice++) {
  88.     xloc = yloc = 0;
  89.     for (block = 0; block < tot_num_blocks; block++) {
  90.  
  91.         if (yloc >= ydim || xloc >= xdim) {
  92.         Error("Error in block creation, try running with the -f option");
  93.         }
  94.         binfo_ptr = &block_info_array[slice][block];
  95.  
  96.         binfo_ptr->xloc = xloc;
  97.         binfo_ptr->yloc = yloc;
  98.  
  99.         binfo_ptr->dslice = data[slice];
  100.         binfo_ptr->grid = grid[slice];
  101.         
  102.         binfo_ptr->width = BLOCK_SIZE;
  103.         binfo_ptr->height = BLOCK_SIZE;
  104.  
  105.         xloc += BLOCK_SIZE;
  106.         if (xloc >= xdim) {
  107.         nxsize = xdim - xloc + BLOCK_SIZE;
  108.         binfo_ptr->width = nxsize;
  109.         xloc = 0;
  110.         yloc += BLOCK_SIZE;
  111.         }
  112.  
  113.         if (binfo_ptr->yloc + binfo_ptr->height >= ydim) {
  114.         if (yloc >= ydim) 
  115.             nysize = ydim - yloc + BLOCK_SIZE;
  116.         else
  117.             nysize = ydim - yloc;
  118.         binfo_ptr->height = nysize;
  119.         }
  120.  
  121.     }
  122.     }
  123. }
  124.  
  125. /*******************************************************************/
  126.  
  127. get_block_min_max(nblocks)
  128.     int       nblocks;
  129. {
  130.     register int i, j;
  131.     register int slice, block;
  132.     register Data_type **dptr;
  133.     BLOCK_INFO *binfo_ptr;
  134.     int       size, xloc, yloc;
  135.     Data_type     dval;
  136.     Data_type     min, max;        /* local min and max variable */
  137.  
  138.     for (slice = 0; slice < zdim; slice++) {
  139.     for (block = 0; block < nblocks; block++) {
  140.         binfo_ptr = &block_info_array[slice][block];
  141.  
  142.         size = binfo_ptr->width * binfo_ptr->height;
  143.         if (size > 0) {
  144.         yloc = binfo_ptr->yloc;
  145.         xloc = binfo_ptr->xloc;
  146.         dptr = binfo_ptr->dslice;
  147.         max = min = dptr[yloc][xloc];
  148.  
  149.         /* use '<=' because cube includes data[+1] */
  150.         for (i = 0; i <= binfo_ptr->height; i++) {
  151.             xloc = binfo_ptr->xloc;
  152.             for (j = 0; j <= binfo_ptr->width; j++) {
  153.             /* dont want `<=` for last block in each row/col */
  154.             if (slice < zdim && yloc < ydim && xloc < xdim) {
  155.                 dval = dptr[yloc][xloc];
  156.                 if (dval > max)
  157.                 max = dval;
  158.                 else if (dval < min)
  159.                 min = dval;
  160.             }
  161.             xloc++;
  162.             }
  163.             yloc++;
  164.         }
  165.         binfo_ptr->min = min;
  166.         binfo_ptr->max = max;
  167.  
  168.         /* set global min/max */
  169.         if (max > data_max)
  170.             data_max = max;
  171.         if (min < data_min)
  172.             data_min = min;
  173.         } else {
  174.         binfo_ptr->min = (Data_type)0;
  175.         binfo_ptr->max = (Data_type)0;
  176.         }
  177.     }
  178.     }
  179.  
  180.     return;
  181. }
  182.  
  183. /********************************************************************/
  184. show_block_grid(tot_num_blocks)   /* for debugging */
  185.     int       tot_num_blocks;
  186. {
  187.     register int slice, block, i,j;
  188.     BLOCK_INFO *binfo;
  189.  
  190.     for (slice = 0; slice < zdim; slice++) {
  191.     for (block = 0; block < tot_num_blocks; block++) {
  192.  
  193.         fprintf(stderr, "\n\n slice: %d,  block: %d ", slice, block);
  194.  
  195.         binfo = &block_info_array[slice][block];
  196.         for (j = 0; j <= binfo->height; j++) {
  197.         fprintf(stderr, "\n");
  198.         for (i = 0; i <= binfo->width; i++) 
  199.             fprintf(stderr, " %2d", binfo->grid[j][i]);
  200.         }
  201.     }
  202.     }
  203. }
  204.  
  205.